package operate;
import book.Book;
import book.BookList;
import user.User;
import java.util.Scanner;
public class State implements IOperate{
    /**
     * 功能：收藏
     * 将此书加入收藏集合（stateBooks）中
     * 并使收藏数++
     */
    public Boolean stateBook(BookList bookList){
        Scanner sc = new Scanner(System.in);
        while(true)
        {
            System.out.println("输入你要收藏的书:");
            String bookName = sc.nextLine();
            Book book = findBook(bookName,bookList);
            if(book!=null)
            {
                for(int i=0;i<bookList.myStateBooks.size();i++)
                {
                    if(bookName.equals(bookList.myStateBooks.get(i).getName()))
                    {
                        System.out.println("你已收藏此书");
                        System.out.println();
                        return false;
                    }
                }
                System.out.println("收藏成功");
                int state = book.getState();
                book.setState(++state);
                bookList.myStateBooks.add(book);
                return true;
            }
            else{
                System.out.println("没有这本书");
                return false;
            }
        }
    }
    @Override
    public void operate(User user, BookList bookList) {
        if(stateBook(bookList)) //收藏成功加积分
        {
            int p = user.getPoint();//收藏成功积分+1
            user.setPoint(++p);
        }
    }

    //查找书籍
    private Book findBook(String bookName,BookList bookList)
    {
        for(int i=0;i<bookList.bookLists.size();i++)
        {
            if(bookName.equals(bookList.bookLists.get(i).getName())) {
                return bookList.bookLists.get(i);
            }
        }
        return null;
    }
}
